<?php
abstract class MY_Controller extends CI_Controller {
	const URI_SEGMENT = 4;
	const PER_PAGE = 3;
	const META_SEPERATE = '|';
	const USER_RESOURCE = '1';
	const ROLE_RESOURCE = '2';
	const ARTICLE_RESOURCE = '3';
	const CATALOG_RESOURCE = '4';
	const PLUGIN_RESOURCE = '5';
	
	
	protected $_controller_path;
	protected $_list_path;
	protected $_view_path;
	protected $_create_path;
	protected $_edit_path;
	protected $_model;
	protected $_parent_model;
	protected $_parent_id;
	protected $_is_search_parent = FALSE;
	protected $_sort_id_type;
	protected $_resource_id;
	protected $_role_id;
	public $model;
	
	function __construct() {
		parent::__construct();
		$this->load->helper('form');
		$this -> load -> model($this -> _model, 'model');
		if ($this -> _is_search_parent) {
			$this -> load -> model($this -> _parent_model, 'parent_model');
		}
		$this->_role_id = $this->session->userdata('role_id');
	}

	function index() {
		$this -> getList();
		// echo $this->model->where('is_active', 1)->count();
		// echo $this->model->count();
	}

	function getList() {	
		 // if($this->zacl->can_read($this->_role_id, $this->_resource_id)==FALSE) {
			// echo "You have no permission to read this resource!";
		 // } else { 	 
		$data = array();
		//pagination
		$config = array();
		$config["base_url"] = site_url() . $this -> _controller_path . '/getList';
		// $config["total_rows"] = $this -> model -> count_by(array('is_active' => 1));
		$config["total_rows"] = $this->model->where('is_active', 1)->count();
		$config["per_page"] = self::PER_PAGE;
		$config["uri_segment"] = self::URI_SEGMENT;

		$this -> pagination -> initialize($config);
		$data["pagination_link"] = $this -> pagination -> create_links();
		$page = $this -> uri -> segment(self::URI_SEGMENT);

		$this -> model -> limit(self::PER_PAGE, $page);
		if(isset($this->_sort_id_type)) {
			$this -> model -> order_by('sort_id', $this->_sort_id_type);
		}
		$this -> model->where('is_active', 1);
		// $data['results'] = $this -> model -> get_many_by(array('is_active' => 1));
		$data['results'] = $this -> model -> get();
		$data['params'] = array();
		if ($this -> _is_search_parent == TRUE) {
			$data['parent_list'] = $this -> parent_model -> get_many_by(array('is_active' => 1));
		}
		$data['meta_search_form'] = $this->generate_meta_search_form($data['params']);
	  $this->render_template($this -> _list_path, $data);
	  // $this->load->view($this -> _list_path, $data);
	  // }
	}
	
	function search() {
		$params = array();
		$meta_data = unsign($this -> input -> post('meta_data'));
		add_to_array($params, array('is_active' => 1));

		$this -> model -> limit(self::PER_PAGE, 0);
		$this -> model -> like('meta_data', $meta_data);
		$data['results'] = $this -> model -> get($params);

		//pagination
		$config = array();
		$config["base_url"] = site_url() . $this -> _controller_path . '/getList';
		$config["total_rows"] = $this->model->where('is_active', 1)->count();
		
		$config["per_page"] = self::PER_PAGE;
		$config["uri_segment"] = self::URI_SEGMENT;
		$this -> pagination -> initialize($config);
		$data["pagination_link"] = $this -> pagination -> create_links();
		//TODO:see later
		$data["params"] = array('meta_data' => $meta_data);
		if ($this -> _is_search_parent == TRUE) {
			add_to_array($data["params"], array($this->_parent_id => ''));
			
			$data['parent_list'] = $this -> parent_model ->where('is_active', 1)->get();
		}
		$data['meta_search_form'] = $this->generate_meta_search_form($data['params']);
		$this->render_template($this -> _list_path, $data);
		// $this->load->view($this -> _list_path, $data);
	}

	public function search_by_parent() {
		$params = array();
		$parent_id = $this -> input -> post($this -> _parent_id);
		if ($parent_id != '') {
			add_to_array($params, array($this -> _parent_id => $parent_id));
		}
		add_to_array($params, array('is_active' => 1));

		//pagination
		$config = array();
		$config["base_url"] = site_url() . $this -> _controller_path . '/getList';
		$config["total_rows"] = $this -> model -> where($params)->count();
		$config["per_page"] = self::PER_PAGE;
		$config["uri_segment"] = self::URI_SEGMENT;
		$this -> pagination -> initialize($config);
		$data["pagination_link"] = $this -> pagination -> create_links();
		$data["params"] = array($this -> _parent_id => $parent_id);
		$this -> model -> limit(self::PER_PAGE, 0);
		$data['results'] = $this -> model -> get_where($params);

		if ($this -> _is_search_parent) {
			// $this -> load -> model('catalog/Catalog_Model', 'catalog_model');
			$data['parent_list'] = $this -> parent_model -> get_where(array('is_active' => 1));
		}
		$data['meta_search_form'] = $this->generate_meta_search_form($data['params']);
		$this->render_template($this -> _list_path, $data);
		// $this->load->view($this -> _list_path, $data);
	}

	function create() {
		$this -> setFormValidation();
		if ($this -> form_validation -> run() == false) {
			$data = array();
			if(isset($this->_parent_model)) {
				$this -> load -> model($this->_parent_model, 'parent_model');
				$data['parent_list'] = $this -> parent_model -> get_where(array('is_active' => 1));
			}
			$this->render_template($this -> _create_path, $data);
			// $this->load->view($this -> _create_path, $data);
		} else {
			$this->save_new_object();
			$this->getList();
		}
	}

	function update() {
		$this -> setFormValidation();

		if ($this -> input -> post('action') == "save") {
			$id = $this -> input -> post('id');
			if ($this -> form_validation -> run() == true) {
				$this->update_object($id);
				$this -> show($id);
			} else {//validation fail
				//TODO: need to fix
				$data = $this -> model ->get_where(array('id' => $id));
				$this->load->view($this -> _edit_path, $data);
			}
		} else {
			$id = $this -> uri -> segment(self::URI_SEGMENT);
			$data = $this -> model ->get_where(array('id' => $id));
			$this->load->view($this -> _edit_path, $data);
		}
	}
	
	public function quick_update() {
		$id = $this -> input -> post('id');
		$this -> setQuickEditFormValidation($id);
		if ($this -> form_validation -> run() == true) {
			$this->quick_update_object($id);
		}
		$this->getList();
	}

	function show($id) {
		$data['result'] = $this -> model ->get_where(array('id' => $id));
		if(isset($this->_parent_model)) {
			$data['parent'] = $this->get_parent($id);
		}
		$this->load->view($this -> _view_path, $data);
	}

	function delete($id) {
		$this->model->where('id', $id)->get();
		$this->model-> is_active = 0;
		$this -> model->save();
		$this -> index();
	}

	function delete_all() {
		$id_array = $this -> input -> post('delete_id');
		$data = array('is_active' => 0, );
		foreach ($id_array as $id) {
			$this->model->where('id', $id)->get();
			$this->model-> is_active = 0;
			$this -> model->save();
		}
		$this -> index();
	}
	
	private function generate_meta_search_form($params) {
		$form = '';
		$form .= form_open($this->_controller_path.'/search');
		$form .='<table>';
		  $form .= '<tr>';
		  if(isset($params) && array_key_exists('meta_data', $params)) {
		  	$meta_data = $params['meta_data'];
		    $form .= '<td><input type="text" name="meta_data" value="'.$meta_data.'"/></td><td><input type="submit" name="seach" value="Seach"/></td>';
		  } else {
		  	$form .= '<td><input type="text" name="meta_data" value=""/></td><td><input type="submit" name="seach" value="Seach"/></td>';
		  }
		  $form .= '</tr>';
		  $form .= '</table>';
		$form .= form_close();
		return $form;
	}
	

	protected function render_template($view_file, $view_data) {
	  $this->template->write('title', 'CI CMS!');
	  $this->template->write_view('content', $view_file, $view_data, FALSE);
	  $this->template->write('sidebar', '<h2>Sidebar</h2>');
	  $this->template->write('footer', '<h2>Footer</h2>');
      $this->template->render();
	}

	abstract protected function setFormValidation();
	
	protected function setQuickEditFormValidation($id) {
	}

	abstract protected function init_input_data();
	
	protected function init_quick_input_data($id) {
	}
	
	protected function get_parent($id) {}
	
	
	protected function save_new_object() {
	}
	
	protected function update_object() {}
	
	protected function quick_update_object() {}
}
?>